﻿using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaiTu.CachingClientManager.RedisClientManager
{
    /// <summary>
    /// Redis 客户端链接提供程序 提供客户端链接 单独提供是为了config和具体的使用redis之间的解耦
    /// </summary>
    public static class RedisFactory
    {     
        private static PooledRedisClientManager mgr = null;
        private static object lockObj = new object();
        private static ConfigInfo.RedisConfigInfo configInfo = null;
        /// <summary>
        /// 从线程池中获取一个客户端代理 只写
        /// </summary>
        /// <returns></returns>
        public static RedisClient GetRedisClient()
        {
            RedisClient client = null;

            lock (lockObj)
            {
                client = GetPooledRedisClientManager().GetClient() as RedisClient;
            }
            return client;
        }
        /// <summary>
        /// 获取只读的RedisClientManager
        /// </summary>
        /// <returns></returns>
        public static RedisClient GetReadOnlyClient()
        {
            RedisClient client = null;

            lock (lockObj)
            {
                client = GetPooledRedisClientManager().GetReadOnlyClient() as RedisClient;
            }
            return client;
        }
        /// <summary>
        /// 获取只读的缓存客户端链接
        /// </summary>
        /// <returns></returns>
        public static RedisClient GetReadOnlyCacheClient()
        {
            RedisClient client = null;

            lock (lockObj)
            {
                client = GetPooledRedisClientManager().GetReadOnlyCacheClient() as RedisClient;
            }
            return client;
        }
        /// <summary>
        /// 获取可写的缓存客户端链接
        /// </summary>
        /// <returns></returns>
        public static RedisClient GetCacheClient()
        {
            RedisClient client = null;

            lock (lockObj)
            {
                client = GetPooledRedisClientManager().GetCacheClient() as RedisClient;
            }
            return client;
        }
        private static ConfigInfo.RedisConfigInfo RedisConfigInfo
        {
            get
            {
                if (configInfo == null)
                {
                    configInfo = Config.RedisConfig.GetConfig();
                }
                return configInfo;
            }
        }
        private static PooledRedisClientManager GetPooledRedisClientManager()
        {
            if (mgr == null)
            {
                mgr = new PooledRedisClientManager(

                                             RedisConfigInfo.WriteServerList.Split(',').ToArray(),
                                             string.IsNullOrEmpty(RedisConfigInfo.ReadServerList) ? RedisConfigInfo.WriteServerList.Split(',') : RedisConfigInfo.ReadServerList.Split(','),
                                             new RedisClientManagerConfig()
                                             {
                                                 AutoStart = true,
                                                 MaxReadPoolSize = RedisConfigInfo.MaxReadPoolSize,
                                                 MaxWritePoolSize = RedisConfigInfo.MaxWritePoolSize
                                             });
            }
            return mgr;
        }

    }


}
